﻿using Common.Infrastructure;
using System.Collections.Generic;
using System.Linq;

namespace Common.EF.Structs
{
    public class EntityConfig
    {
        private List<EntityFieldConfig> fields = null;

        public EntityConfig()
        {
            fields = new List<EntityFieldConfig>(); 
        }

        public List<EntityFieldConfig> Fields
        {
            get { return fields; }
        }

        public string Name { get; set; }

        public EntityConfigContextTypeEnum ContextType { get; set; }

        public List<string> AllowContexts { get; set; }

        public void AddFieldEntityConfig(EntityFieldConfig fieldConfig)
        {
            if (string.IsNullOrEmpty(fieldConfig.Name))
            {
                return;
            }

            var old = Fields.FirstOrDefault(t => string.Compare(t.Name, fieldConfig.Name, true) == 0);
            if (old != null)
            {
                Fields.Remove(old);
            }

            Fields.Add(fieldConfig);
        }

        public EntityFieldConfig GetFieldConfigByName(string name)
        {
            return Fields.FirstOrDefault(t => string.Compare(t.Name, name, true) == 0);
        } 
    }
}
